<?php

namespace App\Models;

use App\Supports\BuyVip\VipPurchaseCanceller;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\VipPurchase
 *
 * @property int $id
 * @property int $user_id
 * @property string $number order number of this upgrade
 * @property string $total_amount
 * @property string|null $paid_at
 * @property string|null $paid_method method of payment
 * @property string|null $paid_number payment number of third party
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @property-read \App\Models\User $user
 * @method static \Database\Factories\VipPurchaseFactory factory(...$parameters)
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase query()
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase whereNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase wherePaidAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase wherePaidMethod($value)
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase wherePaidNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase whereTotalAmount($value)
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase whereUserId($value)
 * @mixin \Eloquent
 * @property array|null $adapay_data adapay pay_info
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase whereAdapayData($value)
 * @property string|null $cancelled_at
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase whereCancelledAt($value)
 * @property string|null $refund_number
 * @property string $refund_status
 * @property string|null $refund_failed_reason
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase whereRefundFailedReason($value)
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase whereRefundNumber($value)
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase whereRefundStatus($value)
 * @property string|null $closed_at
 * @method static \Illuminate\Database\Eloquent\Builder|VipPurchase whereClosedAt($value)
 */
class VipPurchase extends Model
{
    use HasFactory;
    
    protected $guarded = ['id'];
	
	protected $casts = [
		'adapay_data' => 'json'
	];
    
    protected $dates = [
    	'paid_at'
    ];
    
    public function isPaid()
    {
    	return (bool)$this->paid_at;
    }
    
    public function isClosed()
    {
    	return (bool)$this->closed_at;
    }
    
	public function isCancelled()
	{
		return (bool)$this->cancelled_at;
	}
 
	public function cancelByAdmin()
	{
		app(VipPurchaseCanceller::class)->cancelByAdmin($this);
	}
    
    public function user()
    {
    	return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function vipCategory()
    {
        return $this->belongsTo(VipCategory::class, 'vip_category_id', 'id');
    }
}
